﻿using System;
using System.Collections.Generic;
using RiskMan.DataClass.Models;
using System.Xml.Serialization;
using System.IO;
using System.Collections.ObjectModel;
using System.Reflection;
using RiskMan.DataClass.Stops;

namespace RiskMan.Config
{
    public class XmlHistory
    {
        public static String FilenameStoplist = "\\StopList.xml";
        public static string FilenameQuikitems = "\\QuikSettings.xml";
        public static string FilenameWindow = "\\Window.xml";


        public static object GetXmlData(string filename)
        {
            String path = Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments) + "\\Robot";

            if (!Directory.Exists(path))
            {
                DirectoryInfo dir = new DirectoryInfo(path);
                dir.Create();
            }

            if (filename == FilenameWindow)
            {
                try
                {
                    XmlSerializer reader = new XmlSerializer(typeof(List<string>));
                    StreamReader file = new StreamReader(path + filename);
                    List<string> result = new List<string>();
                    result = (List<string>)reader.Deserialize(file);
                    return result;
                }
                catch (Exception)
                {
                    return new List<string>();
                }
            }

           
            if (filename == FilenameQuikitems)
            {
                try
                {
                    XmlSerializer reader = new XmlSerializer(typeof(QuikItems));
                    StreamReader file = new StreamReader(path + filename);
                    QuikItems result = new QuikItems();
                    result = (QuikItems)reader.Deserialize(file);
                    for (int i = 0; i < result.Count; i++)
                    {
                        result[i].IsConn = false;
                    }
                    return result;
                }
                catch (Exception ex)
                {
                    Cfg.SetLog(ex.ToString(), new XmlHistory().GetType(), MethodBase.GetCurrentMethod());
                    return new QuikItems();
                }
            }

            if (filename == FilenameStoplist)
            {
                try
                {
                    XmlSerializer reader = new XmlSerializer(typeof(List<StopsModel>));
                    StreamReader file = new StreamReader(path + filename);
                    List<StopsModel> result = new List<StopsModel>();
                    result = (List<StopsModel>)reader.Deserialize(file);

                    ObservableCollection<StopsModel> res = new ObservableCollection<StopsModel>();

                    for (int i = 0; i < result.Count; i++)
                    {
                        result[i].IsStart = false;
                        result[i].Check = false;
                        result[i].GuidRobot = Guid.NewGuid();
                        res.Add(result[i]);
                    }
                    return res;

                }
                catch (Exception)
                {
                    return new ObservableCollection<StopsModel>();
                }
            }
         
            return null;
        }

        public static void SetXmlData(string filename, object List)
        {
            String path = Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments) + "\\Robot";

            if (!Directory.Exists(path))
            {
                System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(path);
                dir.Create();
            }


            if (filename == FilenameWindow)
            {
                try
                {
                    XmlSerializer write = new System.Xml.Serialization.XmlSerializer(typeof(List<string>));
                    using (StreamWriter file = new System.IO.StreamWriter(path + filename))
                        write.Serialize(file, List);
                }
                catch (Exception ex)
                {
                    Cfg.SetLog(ex.Message, new XmlHistory().GetType(), MethodBase.GetCurrentMethod());
                }
            }

            if (filename == FilenameQuikitems)
            {
                try
                {
                    XmlSerializer write = new XmlSerializer(typeof(QuikItems));
                    using (StreamWriter file = new StreamWriter(path + filename))
                        write.Serialize(file, List);
                }
                catch (Exception ex)
                {
                    Cfg.SetLog(ex.ToString(), new XmlHistory().GetType(), MethodBase.GetCurrentMethod());
                }
            }

            if (filename == FilenameStoplist)
            {
                try
                {
                    XmlSerializer write = new System.Xml.Serialization.XmlSerializer(typeof(List<StopsModel>));
                    using (StreamWriter file = new System.IO.StreamWriter(path + filename))
                        write.Serialize(file, List);
                }
                catch (Exception ex)
                {
                    Cfg.SetLog(ex.ToString(), new XmlHistory().GetType(), MethodBase.GetCurrentMethod());
                }
            }

        }
    }
}